package com.zyj.myapplication.Activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.zyj.myapplication.Adapter.ShaituCommentAdapter;
import com.zyj.myapplication.Javabean.ShaiTuComment;
import com.zyj.myapplication.Javabean.ShaiTuInfo;
import com.zyj.myapplication.Model.Constant;
import com.zyj.myapplication.Model.ShaituModel;
import com.zyj.myapplication.R;

import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ShaituInfoActivity extends AppCompatActivity {
    private TextView Suser_Name, Suser_Time, Suser_ClubName, Suser_Intro;
    private ImageView shaitu_return, Suser_Pic, Suser_address, Suser_address1, Suser_address2;
    private EditText comment_content;
    private Button comment_button;
    private ShaituCommentAdapter shaiTuCommentAdapter;
    private RecyclerView recyclerView;
    private Context mContext;
    int ID;
    String userid;
    String ShaiTu_Id;
    String related;
    String content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shaituinfo);
        initViews();//初始化
        initEvents();//初始化事件
        getComment(ShaiTu_Id);
    }

    private void initViews() {
        shaitu_return = (ImageView) findViewById(R.id.shaitu_return);
        Suser_Pic = (ImageView) findViewById(R.id.Suser_Pic);
        Suser_address = (ImageView) findViewById(R.id.Suser_address);
        Suser_address1 = (ImageView) findViewById(R.id.Suser_address1);
        Suser_address2 = (ImageView) findViewById(R.id.Suser_address2);
        Suser_Name = (TextView) findViewById(R.id.Suser_Name);
        Suser_Time = (TextView) findViewById(R.id.Suser_Time);
        Suser_ClubName = (TextView) findViewById(R.id.Suser_ClubName);
        Suser_Intro = (TextView) findViewById(R.id.Suser_Intro);
        comment_content = (EditText) findViewById(R.id.shaitu_info_content);
        comment_button = (Button) findViewById(R.id.comment_button);
        recyclerView = (RecyclerView) findViewById(R.id.shaitu_info_recyclerview);
        ID = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getInt("user_id", 0);
        ShaiTu_Id = getIntent().getStringExtra("shaituid");
        related = "1";
        content = "suibian";

    }

    private void initEvents() {
        shaitu_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //在当前页面显示值
        ShaituModel shaituModel = new ShaituModel();
        Call call = shaituModel.getShaiTuInfo(ShaiTu_Id, related);
        call.enqueue(new Callback<ShaiTuInfo>() {
            @Override
            public void onResponse(Call<ShaiTuInfo> call, Response<ShaiTuInfo> response) {
                ShaiTuInfo shaiTuInfo = response.body();
                userid = shaiTuInfo.getUserid();
                if (shaiTuInfo.getShaier() == null) {
                    Suser_Name.setText("zhouyanjun");
                    Suser_Pic.setImageResource(R.drawable.tx);
                } else {
                    Suser_Name.setText(shaiTuInfo.getShaier().getUser_name());
                    if (shaiTuInfo.getShaier().getPhoto() == null) {
                        Picasso.with(getBaseContext()).load(R.drawable.tx).into(Suser_Pic);
                    } else {
                        Picasso.with(getBaseContext()).load(Constant.PIC_URL + shaiTuInfo.getShaier().getPhoto())
                                .into(Suser_Pic);
                    }
                }
                Suser_Time.setText(shaiTuInfo.getUpdate_time());
                if (!shaiTuInfo.getThumb0().equals("")) {
                    Picasso.with(getBaseContext()).load(Constant.PIC_URL + shaiTuInfo.getThumb0()).into(Suser_address);
                } else {
                    Picasso.with(getBaseContext()).load(R.drawable.cbeijing).into(Suser_address);
                }
                Intent intent = getIntent();
                String siaddress1 = intent.getStringExtra("Suser_address1");
                String siaddress2 = intent.getStringExtra("Suser_address2");
                String siClubName = intent.getStringExtra("clubname");
                Picasso.with(mContext).load(siaddress1).placeholder(R.drawable.lunbo1).noFade().into(Suser_address1);
                Picasso.with(mContext).load(siaddress2).placeholder(R.drawable.lunbo2).noFade().into(Suser_address2);
                if (siClubName == null) {
                    Suser_ClubName.setText("#东软俱乐部#");
                } else {
                    Suser_ClubName.setText(siClubName);
                }
                if (shaiTuInfo.getTitle().equals("")) {
                    Suser_Intro.setText("今天健身的人真多！！！");
                } else {
                    Suser_Intro.setText(shaiTuInfo.getTitle());
                }
            }

            @Override
            public void onFailure(Call<ShaiTuInfo> call, Throwable t) {

            }
        });

        //获取上一个页面传过来的值
       /* Intent intent = getIntent();
        String username = intent.getStringExtra("username");
        String userphoto = intent.getStringExtra("userpic");
        String sinfotime = intent.getStringExtra("Suser_Time");
        String siaddress = intent.getStringExtra("Suser_address");
        String siaddress1 = intent.getStringExtra("Suser_address1");
        String siaddress2 = intent.getStringExtra("Suser_address2");
        String siClubName = intent.getStringExtra("clubname");
        String sinIntro=intent.getStringExtra("comment");

        Suser_Name.setText(username);
        Suser_Time.setText(sinfotime);
        if (siClubName == null) {
            Suser_ClubName.setText("#东软俱乐部#");
        } else {
            Suser_ClubName.setText(siClubName);
        }
         Suser_Intro.setText(sinIntro);
        Picasso.with(mContext).load(userphoto).placeholder(R.drawable.tx).noFade().into(Suser_Pic);
        Picasso.with(mContext).load(siaddress).into(Suser_address);
        Picasso.with(mContext).load(siaddress1).placeholder(R.drawable.cbeijing).noFade().into(Suser_address1);
        Picasso.with(mContext).load(siaddress2).placeholder(R.drawable.beijing).noFade().into(Suser_address2);*/


        comment_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                content = comment_content.getText().toString();
                if (content.equals("")) {
                    Toast.makeText(ShaituInfoActivity.this, "您输入的信息为空", Toast.LENGTH_LONG).show();
                } else {
                    ShaituModel shaitumodel = new ShaituModel();
                    Call call = shaitumodel.addShaiTuComment(ShaiTu_Id, userid, String.valueOf(ID), content);
                    call.enqueue(new Callback<Integer>() {
                        @Override
                        public void onResponse(Call<Integer> call, Response<Integer> response) {

                            if (response.body() > 0) {
                                Toast.makeText(ShaituInfoActivity.this, "留言发布成功", Toast.LENGTH_LONG).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<Integer> call, Throwable t) {

                        }
                    });
                }
            }
        });

    }

    private void getComment(String shaiTu_Id) {
        ShaituModel shaitumodel = new ShaituModel();
        Call call = shaitumodel.getShaiTuComment(shaiTu_Id);
        call.enqueue(new Callback<List<ShaiTuComment>>() {
            @Override
            public void onResponse(Call<List<ShaiTuComment>> call, Response<List<ShaiTuComment>> response) {

                List<ShaiTuComment> comment = response.body();
                shaiTuCommentAdapter = new ShaituCommentAdapter(ShaituInfoActivity.this, comment);
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ShaituInfoActivity.this);
                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(linearLayoutManager);
                recyclerView.setAdapter(shaiTuCommentAdapter);
                recyclerView.setItemAnimator(new DefaultItemAnimator());

            }

            @Override
            public void onFailure(Call<List<ShaiTuComment>> call, Throwable t) {
                Toast.makeText(ShaituInfoActivity.this, "获取列表失败", Toast.LENGTH_SHORT).show();
            }
        });

    }
}
